package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.adapter.PurDialsListAdapter;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.database.DataService;
import com.aoxon.cargo.shared.DialShared;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.DataUtil;


/**
 * 采购端拨号记录列表页面
 * @author ZZJ
 *
 */
public class PurDialListActivity extends BaseActivity {

	private ImageView ivReturnButton;
	private TextView tvTitle;
	
	private ListView lvSupplierList;
	
	private PurDialsListAdapter adapter;
	
	private Supplier supplier;
	
	private DataService dataService = new DataService(this);
//在本ListView里添加一个View是不太科学的,在这里受到了ListView设置的divider,而出现了下方有两条divider
//	private MyListBottom myListBottom;
	
	private List<Supplier> suppliers = new ArrayList<Supplier>();
	private String deviceId = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setup();
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	
	private void setup(){
		setContentView(R.layout.pur_dials_list);
		supplier = SupIndexShared.getSupplier();
		try {
			TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
			deviceId = telMgr.getDeviceId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		lvSupplierList = (ListView) findViewById(R.id.lvPurDialsList);
		
		
		tvTitle.setText("我的拨号记录");

//		myListBottom = new MyListBottom(this, lvSupplierList);
//		myListBottom.hideListBottom();
		
		
		suppliers = dataService.getSuppliers(deviceId, 0, 0, MyStateUtil.PHONE_RECORD);
		
		adapter = new PurDialsListAdapter(this, suppliers);
		ivReturnButton.setOnClickListener(myClickListener);
		lvSupplierList.setOnItemClickListener(myItemClickListener);
		lvSupplierList.setOnScrollListener(myScrollListener);
		lvSupplierList.setAdapter(adapter);
		
		adapter.notifyDataSetChanged();
	}
	
	
	/**
	 * 滚动监听
	 */
	OnScrollListener myScrollListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
			case OnScrollListener.SCROLL_STATE_FLING:
				adapter.setBusy(true);
				break;
			case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				adapter.setBusy(false);
				break;
			case OnScrollListener.SCROLL_STATE_IDLE:
				adapter.setBusy(false);
				break;
			default:
				break;
			}
			adapter.notifyDataSetChanged();
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {

		}
	};

	
	/**
	 * 选项点击监听
	 */
	OnItemClickListener myItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if ((adapter.getCount() != position)) {
				Intent intent = new Intent(PurDialListActivity.this,
						PurSupplierDetailsActivity.class);
				intent.putExtra("strSupplier",
						DataUtil.gson.toJson(suppliers.get(suppliers.size()-position-1)));
				startActivity(intent);
			}
		}

	};

	OnClickListener myClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.ivPublicTitleReturn:
				CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
				Intent intent = new Intent(getBaseContext(),
						CargoActivity.class);
				startActivity(intent);
				PurDialListActivity.this.finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
			Intent intent = new Intent(getBaseContext(),
					CargoActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

}
